Getting Started with PDF Viewer Control for ASP.NET |
Overview |
|
The EVO PDF Viewer Control for ASP.NET can be used in ASP.NET
applications to display a PDF document inside a ASP.NET page and allows the application
users to browse and print the PDF document. The source of the PDF document can be
a URL or a stream of bytes represented as a byte[] object. The
viewer requires Adobe Reader to be installed on the client computer in order to
work properly. |
Adding the PDF Viewer Control to the Microsoft Visual Studio 2005 Toolbox |
To add the control from EvoPdfViewerAspNet.dll to the Visual Studion Toolbox right click
on the Toolbox panel while you are in the design
mode of a web form and select 'Choose Items'. Browse to the EvoPdfViewerAspNet.dll
and the PdfViewer control should have been added in the Toolbox panel. To use the
control in a web page of your ASP.NET application you simply drag and drop the
PdfViewer control from the toolbox on the web page. As result of this, a reference
to the EvoPdfViewerAspNet.dll is added to your application and the following line is
added to your web page:
<%@ Register Assembly="EvoPdfViewerAspNet" Namespace="EvoPdf.PdfViewerAspNet"
TagPrefix="cc1" %>
A reference to the EvoPdf.PdfViewerAspNet namespace is also added. At this moment you are ready to use the control and display PDF documents inside your ASP.NET page. |
Displaying PDF Documents from Streams |
| In order to display a PDF document represented as a stream of bytes inside your ASP.NET page, you have to register a HTTP handler defined by the PDF viewer control in the Web.config file of your application. This can be done by adding the handler definition in the <system.web> section of the Web.config configuration file as in the example below: |
<system.web> <httpHandlers> <add verb="*" path="PdfBytesHandler.axd" type="EvoPdf.PdfViewerAspNet.PdfSourceBytesHandler,EvoPdfViewerAspNet"></add> </httpHandlers> </system.web> |
| In IIS 7 Integrated Pipeline Mode the syntax and location in Web.config for declaring a HTTP handler was changed like below: |
<!-- IIS 7 Integrated Pipeline Mode --> <system.webServer> <handlers> <add name="PdfBytesHandler" verb="*" path="PdfBytesHandler.axd" type="EvoPdf.PdfViewerAspNet.PdfSourceBytesHandler,EvoPdfViewerAspNet" resourceType="Unspecified"/> </handlers> </system.webServer> |
| We provide the Web.IIS7.config file in the demo application folder that can be renamed to Web.config in order to run the demo application in IIS7 Integrated Pipeline Mode. |
| The next step is to set the PdfSourceBytes property of
the control with a byte[] object representing the binary image of the PDF document.
The byte[] stream can be taken from PDF file or can be produce by other tools like
our PDF converter library, as you can see in the sample ASP.NET application from
the downloaded archive. The byte[] object is stored in the session of the application,
so there is no need to set the PdfSourceBytes property on each postback.
Here is a sample C# code, taken from our sample application, to convert the HTML code from a specified URL to PDF and load the stream into the PDF viewer control when a button is pressed: 1: protected void btnConvert_Click(object sender, EventArgs e) 2: {
3: string urlToConvert = textBoxURLToConvert.Text.Trim(); 4: byte[] pdfBytes = new PdfConverter().GetPdfFromUrlBytes(urlToConvert); 5: PdfViewer1.LicenseKey = "OLant6Kvt6e3ormnt6Smuaalua6urq4="; 6: PdfViewer1.PdfSourceBytes = pdfBytes;
7: }
There is no temporary file in the sample above and this can greatly simplify the deployment of your web applications on various hardware and software configurations. |
Displaying PDF Documents from URLs |
| Displaying a PDF document from a specified URL is as simple as setting the PdfSourceURL property of the PdfViewer control with the URL of
the PDF document to be displayed.
Here is the C# code we are using in the demo ASP.NET application to display the PDF document from a specified URL when a button is pressed: 1: protected void btnDisplayPDF_Click(object sender, EventArgs e) 2: {
3: PdfViewer1.LicenseKey = "OLant6Kvt6e3ormnt6Smuaalua6urq4="; 4: PdfViewer1.PdfSourceURL = textBoxURLToDisplay.Text.Trim();
5: }
|
Running the Samples |
| The samples are located in the Samples folder. To run the samples, open the projects in the Microsoft Visual Studio 2010, 2012 or 2013 and run them from there. |
Licensing |
| The LicenseKey property of the PdfViewer class should be set with the string you received after the purchase of the product. |
Links |
| EVO PDF Viewer for ASP.NET Home Page |